package com.ler.yrmall.ware.listener;

import com.alibaba.fastjson.JSON;
import com.ler.yrmall.order.dto.OrderDTO;
import com.ler.yrmall.ware.entity.WareOrderTaskDetailEntity;
import com.ler.yrmall.ware.entity.WareOrderTaskEntity;
import com.ler.yrmall.ware.service.WareOrderTaskDetailService;
import com.ler.yrmall.ware.service.WareOrderTaskService;
import com.ler.yrmall.ware.service.WareSkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Leron
 */
@Slf4j
@Service
@RocketMQMessageListener(consumerGroup = "ware-order-consumer-group",topic = "order_topic",selectorExpression = "release")
public class OrderReleaseListener implements RocketMQListener<OrderDTO> {

    @Resource
    private WareSkuService wareSkuService;

    @Resource
    private WareOrderTaskService wareOrderTaskService;

    @Resource
    private WareOrderTaskDetailService wareOrderTaskDetailService;

    @Override
    public void onMessage(OrderDTO message) {
        log.info("订单释放:{}", JSON.toJSONString(message));
        WareOrderTaskEntity taskEntity = wareOrderTaskService.getByOrderSn(message.getOrderSn());
        if (taskEntity == null) return;
        List<WareOrderTaskDetailEntity> taskDetailEntityList = wareOrderTaskDetailService.listByTaskId(taskEntity.getId());
        taskDetailEntityList.forEach(item -> {
            wareSkuService.unStockLock(item.getSkuId(),item.getWareId(),item.getId(),item.getSkuNum());
        });
    }

}
